# coding=utf-8
import sys

from PIL import Image
import shutil
import os
'''
图片尺寸处理
'''

class Graphics:
    infile = 'D:\\myimg.jpg'
    outfile = 'D:\\adjust_img.jpg'

    @classmethod
    def fixed_size(cls, width, height):
        """按照固定尺寸处理图片"""
        im = Image.open(cls.infile)
        out = im.resize((width, height), Image.ANTIALIAS)
        out.save(cls.outfile)

    @classmethod
    def resize_by_width(cls, w_divide_h):
        """按照宽度进行所需比例缩放"""
        im = Image.open(cls.infile)
        (x, y) = im.size
        x_s = x
        y_s = x / w_divide_h
        out = im.resize((x_s, y_s), Image.ANTIALIAS)
        out.save(cls.outfile)

    @classmethod
    def resize_by_height(cls, w_divide_h):
        """按照高度进行所需比例缩放"""
        im = Image.open(cls.infile)
        (x, y) = im.size
        x_s = y * w_divide_h
        y_s = y
        out = im.resize((x_s, y_s), Image.ANTIALIAS)
        out.save(cls.outfile)

    @classmethod
    def resize_by_size(cls, size):
        """按照生成图片文件大小进行处理(单位KB)"""
        size *= 1024
        im = Image.open(cls.infile)
        size_tmp = os.path.getsize(cls.infile)
        q = 100
        while size_tmp > size and q > 0:
            print
            q
            out = im.resize(im.size, Image.ANTIALIAS)
            out.save(cls.outfile, quality=q)
            size_tmp = os.path.getsize(cls.outfile)
            q -= 5
        if q == 100:
            shutil.copy(cls.infile, cls.outfile)

    @classmethod
    def cut_by_ratio(cls, width, height):
        """按照图片长宽比进行分割"""
        im = Image.open(cls.infile)
        width = float(width)
        height = float(height)
        (x, y) = im.size
        if width > height:
            region = (0, int((y - (y * (height / width))) / 2), x, int((y + (y * (height / width))) / 2))
        elif width < height:
            region = (int((x - (x * (width / height))) / 2), 0, int((x + (x * (width / height))) / 2), y)
        else:
            region = (0, 0, x, y)

        # 裁切图片
        crop_img = im.crop(region)
        # 保存裁切后的图片
        crop_img.save(cls.outfile)


def resize_image(img_path):
    try:
        mPath, ext = os.path.splitext(img_path)
        if astrcmp(ext, ".png") or astrcmp(ext, ".jpg"):
            img = Image.open(img_path)
            (width, height) = img.size

            if width != new_width:
                new_height = int(height * new_width / width)
                out = img.resize((new_width, new_height), Image.ANTIALIAS)
                new_file_name = '%s%s' % (mPath, ext)
                out.save(new_file_name, quality=100)
                print("图片尺寸修改为：" + str(new_width))
            else:
                print("图片尺寸正确，未修改")
        else:
            print("非图片格式")
    except Exception as e:
        print(e)


def printFile(dirPath):
    print("file: " + dirPath)
    resize_image(dirPath)
    return True


if __name__ == '__main__':
    path = "E:\pp\icon_setting.png"
    new_width = 50
    try:
        b = printFile(path)
        print("\r\n     **********\r\n" + "*********图片处理完毕*********" + "\r\n     **********\r\n")
    except:
        print("Unexpected error:", sys.exc_info())
